<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="misc.html" />
<link rel="prev" href="compiler.html" />
<link rel="parent" href="lib.html" />
<link rel="next" href="node891.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>32. Abstract Syntax Trees</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="31.5 bytecode Generation"
  href="node889.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="Python library Reference"
  href="lib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="32.1 abstract Grammar"
  href="node891.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node889.html">31.5 Bytecode Generation</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="lib.html">Python Library Reference</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node891.html">32.1 Abstract Grammar</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0034000000000000000000"></a><a name="ast"></a>
<br>
32. Abstract Syntax Trees
</h1>

<p>

<p>

<span class="versionnote">New in version 2.5.</span>

<p>
The <code>_ast</code> module helps Python applications to process
trees of the Python abstract syntax grammar. The Python compiler
currently provides read-only access to such trees, meaning that
applications can only create a tree for a given piece of Python
source code; generating byte code from a (potentially modified)
tree is not supported. The abstract syntax itself might change with
each Python release; this module helps to find out programmatically
what the current grammar looks like.

<p>
An abstract syntax tree can be generated by passing <code>_ast.PyCF_ONLY_AST</code>
as a flag to the <tt class="function">compile</tt> builtin function. The result will be a tree
of objects whose classes all inherit from <code>_ast.AST</code>.

<p>
The actual classes are derived from the <code>Parser/Python.asdl</code> file,
which is reproduced below. There is one class defined for each left-hand
side symbol in the abstract grammar (for example, <code>_ast.stmt</code> or <code>_ast.expr</code>).
In addition, there is one class defined for each constructor on the
right-hand side; these classes inherit from the classes for the left-hand
side trees. For example, <code>_ast.BinOp</code> inherits from <code>_ast.expr</code>.
For production rules with alternatives (aka "sums"), the left-hand side
class is abstract: only instances of specific constructor nodes are ever
created.

<p>
Each concrete class has an attribute <code>_fields</code> which gives the
names of all child nodes.

<p>
Each instance of a concrete class has one attribute for each child node,
of the type as defined in the grammar. For example, <code>_ast.BinOp</code>
instances have an attribute <code>left</code> of type <code>_ast.expr</code>.  
Instances of <code>_ast.expr</code> and <code>_ast.stmt</code> subclasses also
have lineno and col_offset attributes.  The lineno is the line number
of source text (1 indexed so the first line is line 1) and the
col_offset is the utf8 byte offset of the first token that generated
the node.  The utf8 offset is recorded because the parser uses utf8 
internally.

<p>
If these attributes are marked as optional in the grammar (using a
question mark), the value might be <code>None</code>. If the attributes
can have zero-or-more values (marked with an asterisk), the
values are represented as Python lists.

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="node891.html">32.1 Abstract Grammar</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="31.5 bytecode Generation"
  href="node889.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="Python library Reference"
  href="lib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="32.1 abstract Grammar"
  href="node891.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node889.html">31.5 Bytecode Generation</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="lib.html">Python Library Reference</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node891.html">32.1 Abstract Grammar</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
